<?php
/**
 * LowCalVanilla_MergedFile class test
 *
 * @package Extensions
 * @subpackage test
 * @copyright Damien 2007
 * @author Damien
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */




/**
 * LowCalVanilla File class tests
 * @package Extensions
 */
class TestMergeFiles extends UnitTestCase {

	var $Context;
	var $ArrayFiles;

	function setUp() {
		$this->Context = new TestContext();


		$this->ArrayFiles = array();
		$this->ArrayFiles[] = new LowCalVanilla_File($this->Context,
			$this->Context->Configuration['APPLICATION_PATH'] . 'js/foo.js');
		$this->ArrayFiles[] = new LowCalVanilla_File($this->Context,
			$this->Context->Configuration['APPLICATION_PATH'] . 'js/bar.js');

		$Firefox2 = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
		$_SERVER['HTTP_USER_AGENT'] = $Firefox2;
	}

	function setClient($GzipAllowed) {
		if ($GzipAllowed) {
			$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
		} else {
			$_SERVER['HTTP_ACCEPT_ENCODING'] = '';
		}
	}

	function testConstructor() {
		$Merge = new LowCalVanilla_MergedFile($this->Context, $this->ArrayFiles, 'text/javascript');
		$this->assertTrue($Merge->_Name);
		$this->assertPattern('/.*foo\.js.*bar\.js.*/', $Merge->_Name);
		$this->assertEqual('text/javascript', $Merge->GetMime());
	}

	function testGetContent() {
		$Merge = new LowCalVanilla_MergedFile($this->Context, $this->ArrayFiles, 'text/javascript');
		$Merge->ClearCache();
		$this->setClient(0);
		$Content = $Merge->GetContent();
		$this->assertIdentical("/* test foo */\n/* test bar */\n", $Content);

		$CachePath = array($Merge->_Cache->_Dir, md5($Merge->_Name) . $Merge->_Cache->_Ext);
		$GZipCachePath = array($Merge->_Cache->_Dir, md5($Merge->_Name . 'gzip') . $Merge->_Cache->_Ext);
		$Cache = new LowCalVanilla_File($this->Context, $CachePath);
		$GZipCache = new LowCalVanilla_File($this->Context, $GZipCachePath);
		$this->assertTrue($Cache->Exist());
		$this->assertTrue($GZipCache->Exist());

		$TimeCache = $Cache->GetLastModification();
		$TimeGZipCache = $GZipCache->GetLastModification();
		sleep(1);

		$this->setClient(1);
		$Merge = new LowCalVanilla_MergedFile($this->Context, $this->ArrayFiles, 'text/javascript');
		$Content = $Merge->GetContent();
		$this->assertEqual(gzencode("/* test foo */\n/* test bar */\n"), $Content);
		$this->assertEqual($TimeCache, $Cache->GetLastModification());
		$this->assertEqual($TimeGZipCache, $GZipCache->GetLastModification());
	}

	function testGetDetails() {
		$this->setClient(0);
		$Merge = new LowCalVanilla_MergedFile($this->Context, $this->ArrayFiles, 'text/javascript');
		$Merge->ClearCache();
		$Content = $Merge->GetContent();
		$Details = $Merge->GetDetails();
		$TimeCache = filemtime($Merge->_Cache->_Dir . '/' . md5($Merge->_Name) . $Merge->_Cache->_Ext);
		$TimeGZipCache = filemtime($Merge->_Cache->_Dir . '/' . md5($Merge->_Name . 'gzip') . $Merge->_Cache->_Ext);

		$this->assertPattern('/[A-Z][a-z]{2}, \d\d [A-Z][a-z]{2} \d{4} \d\d:\d\d:\d\d GMT/', $Details['Expires']);
		$this->assertEqual(gmdate('D, d M Y H:i:s', $TimeCache) . ' GMT', $Details['Last-Modified']);
		$this->assertEqual(md5($Merge->_Name), $Details['ETag']);
		$this->assertEqual('text/javascript', $Details['Content-Type']);
		$this->assertEqual(strlen($Content), $Details['Content-Length']);
		$this->assertFalse($Details['Content-Encoding']);

		$this->setClient(1);
		$Merge = new LowCalVanilla_MergedFile($this->Context, $this->ArrayFiles, 'text/javascript');
		$Content = $Merge->GetContent();
		$Details = $Merge->GetDetails();
		$this->assertPattern('/[A-Z][a-z]{2}, \d\d [A-Z][a-z]{2} \d{4} \d\d:\d\d:\d\d GMT/', $Details['Expires']);
		$this->assertEqual(gmdate('D, d M Y H:i:s', $TimeCache) . ' GMT', $Details['Last-Modified']);
		$this->assertEqual(md5($Merge->_Name), $Details['ETag']);
		$this->assertEqual('text/javascript', $Details['Content-Type']);
		$this->assertEqual(strlen($Content), $Details['Content-Length']);
		$this->assertEqual('gzip', $Details['Content-Encoding']);
	}

}